package com.ruoyi.project.system.submit.controller;


import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.project.system.home.domain.MiniBaseFamilyInfoS;
import com.ruoyi.project.system.submit.domain.MiniDetailData;
import com.ruoyi.project.system.submit.domain.MiniSubmitData;
import com.ruoyi.project.system.submit.domain.inputGroups;
import com.ruoyi.project.system.submit.service.IMiniSubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/submit")
public class MiniSubmitController {


    @Autowired
    private IMiniSubmitService miniSubmitService;


    /**
     * 录入功能
     * @param miniSubmitData 录入信息
     * @return 200
     */
    @PostMapping("")
    public Result entering(@RequestBody MiniSubmitData miniSubmitData){
        System.out.println("程序执行了");

        List<inputGroups> dataArray = miniSubmitData.getInputGroups();

        List<MiniDetailData> forArray = new ArrayList<>();

        //数据筛选
        for (com.ruoyi.project.system.submit.domain.inputGroups inputGroups : dataArray) {
            if (inputGroups.getPlantingIncome() != null) {
                forArray.addAll(inputGroups.getPlantingIncome());
            } else if (inputGroups.getAquacultureIncome() != null) {
                forArray.addAll(inputGroups.getAquacultureIncome());
            } else if (inputGroups.getTertiaryIncome() != null) {
                forArray.addAll(inputGroups.getTertiaryIncome());
            } else if (inputGroups.getTransferIncome() != null) {
                forArray.addAll(inputGroups.getTransferIncome());
            } else if (inputGroups.getOtherPropertyIncome() != null) {
                forArray.addAll(inputGroups.getOtherPropertyIncome());
            } else if (inputGroups.getDirectDividendIncome() != null) {
                forArray.addAll(inputGroups.getDirectDividendIncome());
            } else if (inputGroups.getMigrantWorkersIncome() != null) {
                forArray.addAll(inputGroups.getMigrantWorkersIncome());
            } else if (inputGroups.getVillageIncome() != null) {
                forArray.addAll(inputGroups.getVillageIncome());
            } else if (inputGroups.getOtherTransferIncome() != null) {
                forArray.addAll(inputGroups.getOtherTransferIncome());
            }
        }
        List<Integer> index = new ArrayList<>();
        for(int j = 0;j<forArray.size();j++){
            if((!Objects.equals(forArray.get(j).getItemValue(), ""))||(!Objects.equals(forArray.get(j).getItemProduction(), ""))){
                index.add(j);
            }
        }
        System.out.println(index);

        List<MiniDetailData> realArray = new ArrayList<>();
        for (Integer integer : index) {
            realArray.add(forArray.get(integer));
        }

        if(realArray.size()==0){
            return Result.success("请提交数据");
        }

        System.out.println(miniSubmitData.getBaseInfo());
        System.out.println(realArray);

        Date date=new Date();
        int yearBg = Integer.parseInt(miniSubmitData.getBaseInfo().get(3).getData());
        System.out.println("信息所属年份"+yearBg);
        String operateTime = new SimpleDateFormat("yyyy-MM-dd").format(date);

        //获取户编号和人口数
        String fid = miniSubmitData.getBaseInfo().get(0).getData();
        int incomeDataId = miniSubmitService.getIncomeDataId(fid,yearBg);
        System.out.println(incomeDataId);
        Integer numPerson = miniSubmitService.getNumPerson(fid);

        Double expenditureAmount = miniSubmitData.getExpenditureAmount();
        if(expenditureAmount==null){
            expenditureAmount = (double) 0;
        }
        int userId = miniSubmitData.getUserId();



        //将填写信息插入到detail表中
        miniSubmitService.enterDetail(realArray,incomeDataId);
        //通过计算detail表信息更新data表
        miniSubmitService.enterData(fid,yearBg,incomeDataId,operateTime,numPerson,expenditureAmount,userId);
        return Result.success("提交成功");
    }

    /**
     * 回显功能
     * @param miniBaseFamilyInfoS 录入人员基本信息
     * @return 填写信息
     */
    @PostMapping("echoFarmerData")
    public Result echoFarmerData(@RequestBody MiniBaseFamilyInfoS miniBaseFamilyInfoS){
        System.out.println(miniBaseFamilyInfoS.getFid());
        System.out.println(miniBaseFamilyInfoS.getReportYear());
        int i = miniSubmitService.backShowId(miniBaseFamilyInfoS.getFid(), miniBaseFamilyInfoS.getReportYear());
        System.out.println(i);
        List<MiniDetailData> miniDetailData = miniSubmitService.backShowDetail(i,miniBaseFamilyInfoS.getFid(),miniBaseFamilyInfoS.getReportYear());
        return Result.success(miniDetailData);
    }

    /**
     * 回显支出
     * @param miniBaseFamilyInfoS 录入人员基本信息
     * @return 填写信息
     */
    @PostMapping("/expense")
    public Result echoFarmerDataExpense(@RequestBody MiniBaseFamilyInfoS miniBaseFamilyInfoS){
        int i = miniSubmitService.backShowId(miniBaseFamilyInfoS.getFid(), miniBaseFamilyInfoS.getReportYear());
        System.out.println(i);
        Double expense = miniSubmitService.backShowExpense(miniBaseFamilyInfoS.getFid(), miniBaseFamilyInfoS.getReportYear());
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String formatExpense = decimalFormat.format(expense);
        Double realExpense = Double.valueOf(formatExpense);
        System.out.println("支出为"+expense);
        System.out.println("支出为"+formatExpense);
        return Result.success(formatExpense,"回显成功");
    }



}
